<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form-model
        layout="inline"
        :model="queryParam"
      >
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="业务分类">
              <a-select
                show-search
                :filter-option="filterOption"
                v-model="queryParam['business_id']"
                placeholder="请选择业务分类"
                @change="onChange"
              >
                <a-select-option v-for="item in businessOpts" :key="item.business_id" :value="item.business_id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="老版本官方状态">
              <a-select
                show-search
                :filter-option="filterOption"
                v-model="queryParam['filter[state_id]']"
                placeholder="选择老版本官方状态"
              >
                <a-select-option v-for="(item,index) in oldStateOpts" :key="index" :value="index">{{item}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operator">
      <s-table
        bordered
        size="middle"
        ref="table"
        :row-key="(record) => record.cbs_id"
        :columns="infoColumns1"
        :data="infoData1"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template slot="state_name" slot-scope="text,record">
          <a-button :title="record.filename" icon="edit" size="small" type="link" @click="openPDF(record.file)">{{text}}</a-button>
        </template>
        <div slot="title">
          <a-button :disabled="selectedRowKeys.length == 0" type="primary" @click="onEdit">批量修改</a-button>
        </div>
      </s-table>
    </div>
    <a-modal
      title="批量修改官方状态"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model
        ref="stateForm"
        :model="stateForm"
      >
        <a-form-model-item label="选择要变更的新版本官方状态">
          <a-select
            show-search
            :filter-option="filterOption"
            v-model="stateForm.state_id"
            placeholder="选择老版本官方状态"
          >
            <a-select-option v-for="(item,index) in stateOpts" :key="index" :value="index">{{item}}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { Ellipsis, STable } from '@/components'
import businessColumns from '@/columns/patent.process'
import { getState2, getState3, getState4, postState4 } from '@/api/patent.process'

export default {
  components: {
    STable,
    Ellipsis
  },
  name: 'StateMt',
  data () {
    return {
      queryParam: {},
      infoColumns1: businessColumns.information1,
      infoData1: parameters => {
        return getState2(Object.assign(parameters, this.queryParam)).then(res => {
          this.businessOpts = res.result.businessOpts
          return res.result
        })
      },
      businessOpts: {},
      advanced: false,
      selectedRowKeys: [],
      visible: false,
      stateOpts: {},
      oldStateOpts: {},
      stateForm: {}
    }
  },
  created () {
    this.queryParam['filter[category]'] = '20'
  },
  methods: {
    onEdit () {
      const app = this
      const params = {}
      if (app.queryParam['filter[business_id]'] !== '') {
        params['business_id'] = app.queryParam['filter[business_id]']
        params['category'] = app.queryParam['filter[category]']
        getState3(params).then(res => {
          if (res.status === 0) {
            app.stateOpts = res.result.businessStateOpts
            app.visible = true
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      } else {

      }
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetForm () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    onChange (key) {
      const app = this
      const params = {}
      params['business_id'] = key
      params['category'] = '20'
      getState4(params).then(res => {
        app.oldStateOpts = res.result.oldBusinessStateOpts
      })
    },
    onSubmit () {
      const app = this
      const params = {}
      const data = {}
      params['cbs_id'] = this.selectedRowKeys
      params['category'] = this.queryParam['filter[category]']
      data['state_id'] = this.stateForm.state_id
      postState4(params, data).then(res => {
        if (res.status === 0) {
          app.visible = false
          app.$refs.table.refresh(true)
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    openPDF (url) {
      window.open(url)
    }
  }
}
</script>

<style scoped>

</style>
